// pages/user/afterSalesOrderservice/afterSalesOrderservice.js
import orderIndex  from '../../../api/order';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopInfor:null,
    isIphoneX:app.globalData.isIphoneX,

    imgs:[],
    orderDetailId:'',
    orderNo:'',
    reason:'',
    num:1,
    maxNum:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var shopInfor=JSON.parse(options.item)
    var maxNum=1
    maxNum=Number(shopInfor.num)-Number(shopInfor.questionNum||0)

    this.setData({
      shopInfor:shopInfor,
      maxNum:maxNum||1,
    })
    // if(shopInfor.questionId){
    //   this.getQuestionorderInfo()
    // }
  },
  less(){
    var num=this.data.num
    num=num-1<1?1:num-1
    this.setData({
      num:num
    })

  },
  add(){
    var num=this.data.num
    num=num+1>this.data.maxNum?this.data.maxNum:num+1
    this.setData({
      num:num
    })
  },
  getQuestionorderInfo(){
    orderIndex.apiQuestionorderInfo({id:this.data.shopInfor.questionId}).then(res=>{
      this.setData({
        imgs:res.data.img.splic(','),
        orderNo:res.data.orderNo,
        reason:res.data.reason
      })
    })
  },
  setReason(e){
    var reason=e.currentTarget.dataset.reason
    this.setData({
      reason:reason
    })
  },
  upload() {
    var that=this
    wx.chooseImage({
      count: 5, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera', 'album'], //从相册选择
      success: function (res) {
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          let fileUrl=res.tempFilePaths[i]
          wx.uploadFile({
            url: app.globalData.host +'/upload-file',
            name: 'file',
            filePath: fileUrl,
            success: res => {
              var row=JSON.parse(res.data)
              if(row.code==200){
                var imgs=that.data.imgs
                imgs.push(row.data.fileUrl)
                that.setData({
                  imgs:imgs,
                })
              }
            },
            fail: function (res) {
              console.log(res)
              wx.showToast({
                title: '上传失败',
                icon: 'none',
                duration:3000
              });
            },
          });
        }
      }
    })
  },
  submit(){
    var opt={
      imgs:this.data.imgs.join(','),
      orderDetailId:this.data.shopInfor.id,
      orderNo:this.data.shopInfor.orderNo,
      reason:this.data.reason,
      num:this.data.num
    }
    if(!opt.reason){
      wx.showToast({
        title: '请选择申请原因',
      })
      return false
    }
    wx.requestSubscribeMessage({
      tmplIds: ['GKTh6HnI5mmjRkWg3ecaepf_l6zod_T0VdiHL8ZzCcw','SRsRyuTY8dIxP61UcjuhNlTThi9T-_8aaGzYNjutpHg'],
      complete () {
        orderIndex.apiQuestionorderSubmit(opt).then(res=>{
          wx.showToast({
            title: '提交成功',
          })
          setTimeout(()=>{
              wx.redirectTo({
                url: '/pages/user/afterSalesOrder/afterSalesOrder',
              })
          },1000)
        })
       }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})